{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# K Means Clustering Part 2"
      ],
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "import  pylab as pl\n",
        "import datetime as dt\n",
        "from math import sqrt\n",
        "\n",
        "import warnings\n",
        "warnings.filterwarnings(\"ignore\")\n",
        "\n",
        "# yahoo finance used to fetch data \n",
        "import yfinance as yf\n",
        "yf.pdr_override()\n",
        "\n",
        "from sklearn.cluster import KMeans"
      ],
      "outputs": [],
      "execution_count": 1,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-04-07T00:41:22.144Z",
          "iopub.execute_input": "2022-04-07T00:41:22.149Z",
          "iopub.status.idle": "2022-04-07T00:41:23.358Z",
          "shell.execute_reply": "2022-04-07T00:41:23.351Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "stocks = si.tickers_dow()\n",
        "stocks"
      ],
      "outputs": [
        {
          "output_type": "error",
          "ename": "NameError",
          "evalue": "name 'si' is not defined",
          "traceback": [
            "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[1;32m<ipython-input-2-b3022ca3123f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstocks\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msi\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtickers_dow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mstocks\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
            "\u001b[1;31mNameError\u001b[0m: name 'si' is not defined"
          ]
        }
      ],
      "execution_count": 2,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-04-07T00:41:23.366Z",
          "iopub.execute_input": "2022-04-07T00:41:23.371Z",
          "iopub.status.idle": "2022-04-07T00:37:28.222Z",
          "shell.execute_reply": "2022-04-07T00:37:28.230Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "start = dt.datetime(2020, 1, 1)\n",
        "now = dt.datetime.now()\n"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-04-07T00:37:30.620Z",
          "iopub.execute_input": "2022-04-07T00:37:30.623Z",
          "shell.execute_reply": "2022-04-07T00:37:30.635Z",
          "iopub.status.idle": "2022-04-07T00:37:30.628Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "df = yf.download(stocks, start, now)['Adj Close']\n",
        "df.head()"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-04-07T00:37:47.765Z",
          "iopub.execute_input": "2022-04-07T00:37:47.768Z",
          "iopub.status.idle": "2022-04-07T00:37:50.505Z",
          "shell.execute_reply": "2022-04-07T00:37:50.553Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "returns = df.pct_change().mean() * 252\n",
        "variance = df.pct_change().std() * sqrt(252)\n",
        "returns.columns = [\"Returns\"]\n",
        "variance.columns = [\"Variance\"]"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-04-07T00:38:41.490Z",
          "iopub.execute_input": "2022-04-07T00:38:41.493Z",
          "iopub.status.idle": "2022-04-07T00:38:41.500Z",
          "shell.execute_reply": "2022-04-07T00:38:41.516Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "ret_var = pd.concat([returns, variance], axis = 1).dropna()\n",
        "ret_var.columns = [\"Returns\", \"Variance\"]"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-04-07T00:38:55.905Z",
          "iopub.execute_input": "2022-04-07T00:38:55.908Z",
          "shell.execute_reply": "2022-04-07T00:38:55.919Z",
          "iopub.status.idle": "2022-04-07T00:38:55.926Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X =  ret_var.values\n",
        "sse = []\n",
        "\n",
        "for k in range(2,15):\n",
        "    \n",
        "    kmeans = KMeans(n_clusters = k)\n",
        "    kmeans.fit(X)\n",
        "    \n",
        "    sse.append(kmeans.inertia_) #SSE for each n_clusters\n",
        "pl.plot(range(2,15), sse)\n",
        "pl.title(\"Elbow Curve\")\n",
        "pl.subplots()\n",
        "pl.show()"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-04-07T00:39:58.118Z",
          "iopub.execute_input": "2022-04-07T00:39:58.122Z",
          "shell.execute_reply": "2022-04-07T00:39:58.523Z",
          "iopub.status.idle": "2022-04-07T00:39:58.515Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "kmeans = KMeans(n_clusters = 5).fit(X)\n",
        "centroids = kmeans.cluster_centers_\n",
        "pl.scatter(X[:,0],X[:,1], c = kmeans.labels_, cmap =\"rainbow\")\n",
        "pl.show()"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-04-07T00:40:02.008Z",
          "iopub.execute_input": "2022-04-07T00:40:02.013Z",
          "shell.execute_reply": "2022-04-07T00:40:02.101Z",
          "iopub.status.idle": "2022-04-07T00:40:02.108Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X = ret_var.values\n",
        "kmeans =KMeans(n_clusters = 5).fit(X)\n",
        "centroids = kmeans.cluster_centers_\n",
        "pl.scatter(X[:,0],X[:,1], c = kmeans.labels_, cmap =\"rainbow\")\n",
        "pl.show()"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-04-07T00:40:12.616Z",
          "iopub.execute_input": "2022-04-07T00:40:12.620Z",
          "shell.execute_reply": "2022-04-07T00:40:12.703Z",
          "iopub.status.idle": "2022-04-07T00:40:12.709Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "Companies = pd.DataFrame(ret_var.index)\n",
        "cluster_labels = pd.DataFrame(kmeans.labels_)\n",
        "df = pd.concat([Companies, cluster_labels],axis = 1)\n",
        "df.columns = ['Stock', 'Cluster Labels']\n",
        "df.set_index('Stock')"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-04-07T00:41:01.920Z",
          "iopub.execute_input": "2022-04-07T00:41:01.925Z",
          "iopub.status.idle": "2022-04-07T00:41:01.936Z",
          "shell.execute_reply": "2022-04-07T00:41:01.950Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "df"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-04-07T00:41:12.197Z",
          "iopub.execute_input": "2022-04-07T00:41:12.202Z",
          "iopub.status.idle": "2022-04-07T00:41:12.212Z",
          "shell.execute_reply": "2022-04-07T00:41:12.226Z"
        }
      }
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.6.13",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    },
    "kernelspec": {
      "argv": [
        "C:/Users/Tin Hang/Anaconda3\\python.exe",
        "-m",
        "ipykernel_launcher",
        "-f",
        "{connection_file}"
      ],
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "nteract": {
      "version": "0.28.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}